package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.*;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.QiniuCloudUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService service;
    @Autowired
    private ITravelCommendService commendService;
    @Autowired
    private ITravelCommentService commentService;
    @Autowired
    private ITravelDiscussService discussService;
    @Autowired
    private ITravelLikeService travelLikeService;

    @PostMapping
    public Object save(Travel travel) {
        service.saveOrUpdate(travel);
        return null;
    }
    @GetMapping("/{id}")
    public Object getTravel(@PathVariable Long id, Long purpose) {
        //purpose :查询目的,用于标识本次查询是否是阅读内容查询,统计阅读量,1 即为内容查询
        Travel travel = service.selectById(id,purpose);
        return travel;
    }

    @PutMapping("/{id}")
    public Object update(Travel travel){
        service.saveOrUpdate(travel);
        return null;
    }
    @GetMapping
    public Object getReleaseList(TravelQueryObject qo){
        qo.setState(Travel.STATE_RELEASE);
        qo.setOrderBy("createTime desc");
        PageInfo<Travel> result = service.selectForList(qo);
        return result;
    }

    @GetMapping("/commend")
    public Object getTravelCommend(TravelCommendQueryObject qo){
        qo.setOrderBy("schedule desc");
        List<TravelCommend> list = commendService.selectCommendByType(qo);
        return list;
    }

    //根据攻略Id查找攻略所属地区的热门游记
    @GetMapping("/hot/{StrategyId}")
    public Object getRegionHotTravel(TravelQueryObject qo){
        qo.setOrderBy("viewCount desc");
        PageInfo<Travel> result = service.regionHotTravelList(qo);
        return result;
    }






    //回显攻略点评
    @GetMapping("/comment/{travelId}")
    public Object getReleaseList(@ModelAttribute("qo") TravelCommentQueryObject qo, Model model){
        qo.setOrderBy("createTime desc");

        PageInfo<TravelComment> result = commentService.selectList(qo);
        model.addAttribute("result",result);
        return result;
    }

    @GetMapping("/travelComment/{id}")
    public Object getComment(Long id){
        TravelComment travelComment = commentService.selectById(id);

        return travelComment;
    }


    //新增攻略点评
    @PostMapping("/comment")
    public Object insert(TravelComment comment, MultipartFile[] imgs){
        JSONResult result = new JSONResult();
        for (int i = 0;i < imgs.length;i++) {
            //得到图片名
            MultipartFile img = imgs[i];
            System.out.println(img);
            if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
                String url = QiniuCloudUtil.uploadQiniu(img);
                comment.setCoverUrl(url);
            }
        }
        try {
            commentService.saveOrUpdate(comment);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;

    }

    //评论回显系统
    @GetMapping("/travelDiscuss/{commentId}")
    public Object getComment(StrategyDiscussQueryObject qo){
        qo.setOrderBy("createTime desc");
        PageInfo<TravelDiscuss> result = discussService.selectList(qo);
        return result;
    }
    //评论数量回显系统
    @GetMapping("/travelDiscuss/count/{commentId}")
    public Object getCount(Long commentId){
        return discussService.selectCount(commentId);

    }


    //新增攻略评论评论
    @PostMapping("/travelDiscuss")
    public Object insert(TravelDiscuss discuss){
        JSONResult result = new JSONResult();

        try {
            discussService.saveOrUpdate(discuss);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;

    }
    @GetMapping("/likeCount/{travelId}")
    public Object getLikeCount(@PathVariable Long travelId){
        Integer count = travelLikeService.getLikeCount(travelId);
        return count;

    }
}
